package com.example.myframe.utils

import android.graphics.Color
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.TimeUtils
import com.example.myframe.R
/**
 * DataBinding的xml方法
 * */
object MyBindingAdapter {

    @BindingAdapter("intText")
    @JvmStatic
    fun setIntText(tv: TextView, o: Any) {
        if (o is Int || o is Double || o is Float)
            tv.text = java.lang.String.valueOf(o)
    }

    @BindingAdapter("intText")
    @JvmStatic
    fun setIntText(et: EditText, o: Any) {
        if (o is Int || o is Double || o is Float)
            et.setText(java.lang.String.valueOf(o))
    }


    @BindingAdapter("dateFormat")
    @JvmStatic
    fun dateFormat(tv: TextView, pattern: String, date: String) {
        tv.text = TimeUtils.getSafeDateFormat(pattern).format(date)
    }

    @BindingAdapter("stateFlagColorAndText")
    @JvmStatic
    fun stateFlagColorAndText(tv: TextView, stateFlag: String) {
        when (stateFlag) {
            "0" -> {
                tv.text = "录入"
                tv.setTextColor(Color.BLUE)
            }
            "1" -> {
                tv.text = "提交"
                tv.setTextColor(Color.RED)
            }
            "2" -> {
                tv.text = "审核"
                tv.setTextColor(tv.resources.getColor(R.color.result_points))
            }
            "3" -> {
                tv.text = "入账"
                tv.setTextColor(Color.DKGRAY)
            }
            "4" -> {
                tv.text = "指定关闭"
                tv.setTextColor(Color.MAGENTA)
            }
            else -> {
                tv.text = ""
                tv.setTextColor(Color.BLACK)
            }
        }
    }

    @BindingAdapter("stateFlagColorAndText")
    @JvmStatic
    fun stateFlagColorAndText(tv: TextView, stateFlag: Int) {
        when (stateFlag) {
            0 -> {
                tv.text = "录入"
                tv.setTextColor(Color.BLUE)
            }
            1 -> {
                tv.text = "提交"
                tv.setTextColor(Color.RED)
            }
            2 -> {
                tv.text = "审核"
                tv.setTextColor(tv.resources.getColor(R.color.result_points))
            }
            3 -> {
                tv.text = "入账"
                tv.setTextColor(Color.DKGRAY)
            }
            4 -> {
                tv.text = "指定关闭"
                tv.setTextColor(Color.MAGENTA)
            }
            else -> {
                tv.text = ""
                tv.setTextColor(Color.BLACK)
            }
        }
    }

}